home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / DylanTalk / SILIE Source / Sample.c next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  10.5 KB  |  479 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <Resources.h>
  3. #include <QuickDraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Windows.h>
  7. #include <Menus.h>
  8. #include <TextEdit.h>
  9. #include <Dialogs.h>
  10. #include <Desk.h>
  11. #include <ToolUtils.h>
  12. #include <Memory.h>
  13. #include <SegLoad.h>
  14. #include <Files.h>
  15. #include <OSUtils.h>
  16. #include <OSEvents.h>
  17. #include <DiskInit.h>
  18. #include <Packages.h>
  19. #include <Traps.h>
  20. #include <SoundInput.h>
  21. #include <QDOffscreen.h>
  22. #include <SysEqu.h>
  23.  
  24. #include "Sample.h"        /* bring in all the #defines for Sample */
  25.  
  26.  
  27. Boolean        gInBackground;        /* maintained by Initialize and DoEvent */
  28. long         gInRefNum;
  29. short         lastValue = 0;
  30. GWorldPtr    myGWorld;
  31. long        gLastDown;
  32. Boolean        down = false;
  33. short        stringCount = 1;
  34. Rect        gGoRect, gStopRect;
  35.  
  36. #pragma segment Main
  37. main()
  38. {
  39.     UnloadSeg((Ptr) _DataInit);        /* note that _DataInit must not be in Main! */
  40.     
  41.     MaxApplZone();                    /* expand the heap so code segments load at the top */
  42.  
  43.     Initialize();                    /* initialize the program */
  44.     UnloadSeg((Ptr) Initialize);    /* note that Initialize must not be in Main! */
  45.  
  46.     EventLoop();                    /* call the main event loop */
  47.     
  48. }
  49.  
  50.  
  51. #pragma segment Main
  52. void EventLoop()
  53. {
  54.     Boolean        gotEvent;
  55.     EventRecord    event;
  56.  
  57.     do {
  58.         /* use WNE if it is available */
  59.         gotEvent = WaitNextEvent(everyEvent, &event, 0L, nil);
  60.         if ( gotEvent ) {
  61.             DoEvent(&event);
  62.         }
  63.         DoAnalysis(gInRefNum);
  64.     } while ( true );    /* loop forever; we quit via ExitToShell */
  65. } /*EventLoop*/
  66.  
  67.  
  68. #pragma segment Main
  69. void DoEvent(event)
  70.     EventRecord    *event;
  71. {
  72.     short        part;
  73.     WindowPtr    window;
  74.     Boolean        hit;
  75.     char        key;
  76.  
  77.     switch ( event->what ) {
  78.         case mouseDown:
  79.             part = FindWindow(event->where, &window);
  80.             switch ( part ) {
  81.                 case inMenuBar:                /* process a mouse menu command (if any) */
  82.                     AdjustMenus();
  83.                     DoMenuCommand(MenuSelect(event->where));
  84.                     break;
  85.                 case inSysWindow:            /* let the system handle the mouseDown */
  86.                     SystemClick(event, window);
  87.                     break;
  88.                 case inContent:
  89.                     if ( window != FrontWindow() ) {
  90.                         SelectWindow(window);
  91.                         /*DoEvent(event);*/    /* use this line for "do first click" */
  92.                     } else
  93.                         DoContentClick(window);
  94.                     break;
  95.                 case inDrag:                /* pass screenBits.bounds to get all gDevices */
  96.                     DragWindow(window, event->where, &qd.screenBits.bounds);
  97.                     break;
  98.                 case inGrow:
  99.                     break;
  100.                 case inZoomIn:
  101.                 case inZoomOut:
  102.                     hit = TrackBox(window, event->where, part);
  103.                     if ( hit ) {
  104.                         SetPort(window);                /* the window must be the current port... */
  105.                         EraseRect(&window->portRect);    /* because of a bug in ZoomWindow */
  106.                         ZoomWindow(window, part, true);    /* note that we invalidate and erase... */
  107.                         InvalRect(&window->portRect);    /* to make things look better on-screen */
  108.                     }
  109.                     break;
  110.             }
  111.             break;
  112.         case keyDown:
  113.         case autoKey:                        /* check for menukey equivalents */
  114.             key = event->message & charCodeMask;
  115.             if ( event->modifiers & cmdKey )            /* Command key down */
  116.                 if ( event->what == keyDown ) {
  117.                     AdjustMenus();                        /* enable/disable/check menu items properly */
  118.                     DoMenuCommand(MenuKey(key));
  119.                 }
  120.             break;
  121.         case activateEvt:
  122.             DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
  123.             break;
  124.         case updateEvt:
  125.             DoUpdate((WindowPtr) event->message);
  126.             break;
  127.         case osEvt:
  128.             switch ((event->message >> 24) & 0x0FF) {        /* high byte of message */
  129.                 case suspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  130.                     gInBackground = (event->message & resumeFlag    ) == 0;
  131.                     DoActivate(FrontWindow(), !gInBackground);
  132.                     break;
  133.             }
  134.             break;
  135.     }
  136. } /*DoEvent*/
  137.  
  138. #pragma segment Main
  139. void DoUpdate(window)
  140.     WindowPtr    window;
  141. {
  142.     BeginUpdate(window);                /* this sets up the visRgn */
  143.     if ( ! EmptyRgn(window->visRgn) )    /* draw if updating needs to be done */
  144.         DrawWindow(window);
  145.     EndUpdate(window);
  146. } /*DoUpdate*/
  147.  
  148. #pragma segment Main
  149. void DoActivate(window, becomingActive)
  150.     WindowPtr    window;
  151.     Boolean        becomingActive;
  152. {
  153.     if ( becomingActive )
  154.         /* do whatever you need to at activation */ ;
  155.     else
  156.         /* do whatever you need to at deactivation */ ;
  157. } /*DoActivate*/
  158.  
  159.  
  160. #pragma segment Main
  161. void DoContentClick(window)
  162.     WindowPtr    window;
  163. {
  164. } /*DoContentClick*/
  165.  
  166.  
  167. #pragma segment Main
  168. void DrawWindow(window)
  169.     WindowPtr    window;
  170. {
  171.     SetPort(window);
  172.  
  173.     EraseRect(&window->portRect);    /* clear out any garbage that may linger */
  174. } /*DrawWindow*/
  175.  
  176. #pragma segment Main
  177. void DoMenuCommand(menuResult)
  178.     long        menuResult;
  179. {
  180.     short        menuID;                /* the resource ID of the selected menu */
  181.     short        menuItem;            /* the item number of the selected menu */
  182.     short        itemHit;
  183.     Str255        daName;
  184.     short        daRefNum;
  185.     Boolean        handledByDA;
  186.  
  187.     menuID = HiWord(menuResult);    /* use macros for efficiency to... */
  188.     menuItem = LoWord(menuResult);    /* get menu item number and menu number */
  189.     switch ( menuID ) {
  190.         case mApple:
  191.             switch ( menuItem ) {
  192.                 case iAbout:        /* bring up alert for About */
  193.                     itemHit = Alert(rAboutAlert, nil);
  194.                     break;
  195.                 default:            /* all non-About items in this menu are DAs */
  196.                     GetItem(GetMHandle(mApple), menuItem, daName);
  197.                     daRefNum = OpenDeskAcc(daName);
  198.                     break;
  199.             }
  200.             break;
  201.         case mFile:
  202.             switch ( menuItem ) {
  203.                 case iClose:
  204.                     DoCloseWindow(FrontWindow());
  205.                     break;
  206.                 case iQuit:
  207.                     Terminate();
  208.                     break;
  209.             }
  210.             break;
  211.         case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  212.             handledByDA = SystemEdit(menuItem-1);    /* since we don’t do any Editing */
  213.             break;
  214.     }
  215.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  216. } /*DoMenuCommand*/
  217.  
  218.  
  219. #pragma segment Main
  220. Boolean DoCloseWindow(window)
  221.     WindowPtr    window;
  222. {
  223.     CloseWindow(window);
  224.     return true;
  225. } /*DoCloseWindow*/
  226.  
  227. #pragma segment Main
  228. void Terminate()
  229. {
  230.     WindowPtr    aWindow;
  231.     Boolean        closed;
  232.     FInfo        theInfo;
  233.     
  234.     closed = true;
  235.  
  236.     GetFInfo("\pBrita", 0, &theInfo);
  237.     theInfo.fdFlags -= fInvisible;
  238.     SetFInfo("\pBrita", 0, &theInfo);
  239.     CloseDevice(gInRefNum);
  240.  
  241.     do {
  242.         aWindow = FrontWindow();                /* get the current front window */
  243.         if (aWindow != nil)
  244.             closed = DoCloseWindow(aWindow);    /* close this window */    
  245.     }
  246.     while (closed && (aWindow != nil));
  247.     if (closed)
  248.         ExitToShell();                            /* exit if no cancellation */
  249. } /*Terminate*/
  250.  
  251. #pragma segment Initialize
  252. void Initialize()
  253. {
  254.     Handle        menuBar;
  255.     WindowPtr    window;
  256.     EventRecord event;
  257.     short        count;
  258.     OSErr    err;
  259.     short    i;
  260.     CGrafPtr    port, currPort;
  261.     GDHandle    gdh, currDevice;
  262.     Rect        picRect;
  263.  
  264.     gInBackground = false;
  265.  
  266.     InitGraf((Ptr) &qd.thePort);
  267.     InitFonts();
  268.     InitWindows();
  269.     InitMenus();
  270.     TEInit();
  271.     InitDialogs(nil);
  272.     InitCursor();
  273.     
  274.     for (count = 1; count <= 3; count++)
  275.         EventAvail(everyEvent, &event);
  276.     
  277.     window = GetNewCWindow(rWindow, nil, (WindowPtr) -1);
  278.     SetPort(window);
  279.     TextFont(times);
  280.     TextSize(18);
  281.     
  282.     SetRect(&gStopRect, 270, 10, 370, 110);
  283.     SetRect(&gGoRect, 270, 120, 370, 220);
  284.     
  285.     if ( down )                    /* draw a red (or white) stop light */
  286.         ForeColor(redColor);
  287.     else
  288.         ForeColor(whiteColor);
  289.     PaintOval(&gStopRect);
  290.     ForeColor(blackColor);
  291.     FrameOval(&gStopRect);
  292.     if ( ! down )                /* draw a green (or white) go light */
  293.         ForeColor(greenColor);
  294.     else
  295.         ForeColor(whiteColor);
  296.     PaintOval(&gGoRect);
  297.     ForeColor(blackColor);
  298.     FrameOval(&gGoRect);
  299.  
  300.     ValidRect(&(window->portRect));
  301.     
  302.     SetRect(&picRect,0,0,250*40,150);
  303.     err = NewGWorld(&myGWorld,8,&picRect,0L,0L,0);
  304.     LockPixels(GetGWorldPixMap(myGWorld));
  305.     
  306.     GetGWorld(&currPort, &currDevice);
  307.     SetGWorld(myGWorld,0L);
  308.             
  309.     SetRect(&picRect,0,0,250,150);
  310.     for (i = 0;i <= 32;i++)
  311.     {
  312.         PicHandle     pic;
  313.  
  314.         pic = (PicHandle)GetResource('PICT',i+128);
  315.         if (pic)
  316.         {
  317.             DrawPicture(pic,&picRect);
  318.             OffsetRect(&picRect, 250, 0);
  319.         }
  320.         ReleaseResource((Handle) pic);
  321.     }
  322.     
  323.     SetGWorld(currPort,currDevice);
  324.  
  325.  
  326.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  327.     SetMenuBar(menuBar);                    /* install menus */
  328.     DisposHandle(menuBar);
  329.     AddResMenu(GetMHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  330.     DrawMenuBar();
  331.     
  332.     err = TurnOnContinuousRecordingAndLevelMetering(&gInRefNum);
  333.     
  334. } /*Initialize*/
  335.  
  336.  
  337. #pragma segment Main
  338. void AlertUser()
  339. {
  340.     short        itemHit;
  341.  
  342.     SetCursor(&qd.arrow);
  343.     itemHit = Alert(rUserAlert, nil);
  344.     ExitToShell();
  345. } /* AlertUser */
  346.  
  347. void AdjustMenus( void )
  348. {
  349.  
  350. }
  351.  
  352. OSErr TurnOnContinuousRecordingAndLevelMetering(long *inRefNum)
  353. {
  354.     SPB pb;
  355.     OSErr err;
  356.     Str255 deviceName;
  357.     short    array[12];
  358.             
  359.     if (err = SPBOpenDevice( 0L, siWritePermission, inRefNum)) return err;
  360.     if (err =  SPBSetDeviceInfo(*inRefNum, siContinuous, (Ptr)array)) return err;
  361.     array[0] = 1;
  362.     array[1] = 0;
  363.     if (err = SPBSetDeviceInfo(*inRefNum, siLevelMeterOnOff,  (Ptr)array)) return err;
  364.     
  365.     return err;
  366. }
  367.  
  368. OSErr CloseDevice(long inRefNum)
  369. {
  370.     OSErr err;
  371.     
  372.     err = SPBCloseDevice( inRefNum);
  373.     return err;
  374. }
  375.  
  376. short GetMeterValue(long inRefNum)
  377. {
  378.     SPB pb;
  379.     OSErr err;
  380.     short    array[12];
  381.             
  382.     if (err = SPBGetDeviceInfo(inRefNum, siLevelMeterOnOff,  (Ptr)array)) return err;
  383.     return (array[1]);
  384. }
  385.  
  386. void DoAnalysis(long inRefNum)
  387. {
  388.     short value;
  389.     Handle thepic;
  390.     Rect  theRect;
  391.     Str255 numstr;
  392.     Str255 theString;
  393.     
  394.     value = GetMeterValue(inRefNum);
  395.     value /= 8;
  396.     if (lastValue + 3 < value)
  397.     {
  398.         lastValue+= 3;
  399.     }
  400.     else if (lastValue - 3 > value)
  401.     {
  402.         lastValue -= 3;
  403.     }
  404.     else
  405.         lastValue = value;
  406.     
  407.     if (lastValue < 0)
  408.         lastValue = 0;
  409.  
  410.     if (lastValue < 8)
  411.     {
  412.         lastValue = 8;
  413.         if ( (!down) && ((TickCount() - gLastDown) > 90))
  414.         {
  415.             if (stringCount < 13) {
  416.                 GetIndString(theString, 128, stringCount++);
  417.                 SetRect(&theRect, 10, 170, 260, 300);
  418.                 TextBox(&(theString[1]), theString[0], &theRect, teJustLeft);
  419.             }
  420.             else
  421.                 SysBeep(1);
  422.             down = true;
  423.             if ( down )                    /* draw a red (or white) stop light */
  424.                 ForeColor(redColor);
  425.             else
  426.                 ForeColor(whiteColor);
  427.             PaintOval(&gStopRect);
  428.             ForeColor(blackColor);
  429.             FrameOval(&gStopRect);
  430.             if ( ! down )                /* draw a green (or white) go light */
  431.                 ForeColor(greenColor);
  432.             else
  433.                 ForeColor(whiteColor);
  434.             PaintOval(&gGoRect);
  435.             ForeColor(blackColor);
  436.             FrameOval(&gGoRect);
  437.  
  438.         }
  439.     }
  440.     else
  441.     {
  442.         if (down)
  443.         {
  444.             down = false;
  445.             if ( down )                    /* draw a red (or white) stop light */
  446.                 ForeColor(redColor);
  447.             else
  448.                 ForeColor(whiteColor);
  449.             PaintOval(&gStopRect);
  450.             ForeColor(blackColor);
  451.             FrameOval(&gStopRect);
  452.             if ( ! down )                /* draw a green (or white) go light */
  453.                 ForeColor(greenColor);
  454.             else
  455.                 ForeColor(whiteColor);
  456.             PaintOval(&gGoRect);
  457.             ForeColor(blackColor);
  458.             FrameOval(&gGoRect);
  459.         
  460.         }
  461.         gLastDown = TickCount();
  462.     };
  463.     
  464.     {
  465.         WindowPtr firstWindow, secondWindow;
  466.         Rect      destRect, srcRect;
  467.         
  468.         firstWindow = FrontWindow();
  469.         SetPort(firstWindow);
  470.         
  471.         SetRect(&srcRect,0,0,250,150);
  472.         destRect = srcRect;
  473.         OffsetRect(&destRect, 10, 10);
  474.         OffsetRect(&srcRect, lastValue * 250, 0);
  475.         CopyBits((BitMapPtr) *(GetGWorldPixMap(myGWorld)),&((qd.thePort)->portBits),&srcRect,&destRect,srcCopy,0L);
  476.     
  477.     }
  478. }
  479.